package com.fourthGroup.config;

import com.fourthGroup.model.Activity;
import com.fourthGroup.model.Coupon;
import com.fourthGroup.service.ActivityService;
import com.fourthGroup.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

@Component
@Slf4j
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private CouponService couponService;

    @Override
    public void run(String... args) {
        log.info("初始化活动和优惠券数据...");
        initializeData();
    }

    private void initializeData() {
        // 创建满减优惠券
        Coupon coupon1 = new Coupon();
        coupon1.setCouponCode("DISCOUNT10");
        coupon1.setCouponName("满100减10元");
        coupon1.setCouponType(1); // 满减券
        coupon1.setDiscount(10.0); // 减10元
        coupon1.setThreshold(100.0); // 满100元
        coupon1.setTotal(100); // 100张
        
        // 设置开始和结束时间
        Calendar calendar = Calendar.getInstance();
        Date now = calendar.getTime();
        
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        coupon1.setStartTime(calendar.getTime());
        
        calendar.add(Calendar.DAY_OF_MONTH, 30);
        coupon1.setEndTime(calendar.getTime());
        
        couponService.createCoupon(coupon1);
        
        // 创建折扣优惠券
        Coupon coupon2 = new Coupon();
        coupon2.setCouponCode("PERCENT80");
        coupon2.setCouponName("8折优惠券");
        coupon2.setCouponType(2); // 折扣券
        coupon2.setDiscount(0.8); // 8折
        coupon2.setThreshold(0.0); // 无门槛
        coupon2.setTotal(50); // 50张
        
        calendar = Calendar.getInstance();
        
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        coupon2.setStartTime(calendar.getTime());
        
        calendar.add(Calendar.DAY_OF_MONTH, 15);
        coupon2.setEndTime(calendar.getTime());
        
        couponService.createCoupon(coupon2);
        
        // 创建秒杀活动
        Activity activity1 = new Activity();
        activity1.setActivityName("双11秒杀活动");
        activity1.setActivityDesc("双11满100减10元优惠券秒杀");
        
        calendar = Calendar.getInstance();
        
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        activity1.setStartTime(calendar.getTime());
        
        calendar.add(Calendar.DAY_OF_MONTH, 2);
        activity1.setEndTime(calendar.getTime());
        
        activity1.setCouponId(1L);
        
        activityService.createActivity(activity1);
        
        // 创建折扣活动
        Activity activity2 = new Activity();
        activity2.setActivityName("新年特惠活动");
        activity2.setActivityDesc("新年8折优惠券限量发放");
        
        calendar = Calendar.getInstance();
        
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        activity2.setStartTime(calendar.getTime());
        
        calendar.add(Calendar.DAY_OF_MONTH, 10);
        activity2.setEndTime(calendar.getTime());
        
        activity2.setCouponId(2L);
        
        activityService.createActivity(activity2);
        
        log.info("初始化数据完成，创建了2张优惠券和2个活动");
    }
} 